package repository

import (
	"errors"
	"github.com/jinzhu/gorm"
	"go_shengshu_admin/model"
	"go_shengshu_admin/utils"
	"time"
)

type CategoryRepository struct {
	DB *gorm.DB
}

type CategoryRepoInterface interface {
	List() (categorys []*model.CategoryResult, err error)
	GetTotal() (total int64, err error)
	Get(id string) (categorys []*model.CategoryResult, err error)
	Exist(category model.Category) *model.Category
	ExistByCategoryId(id string) *model.Category
	Add(category model.Category) (bool, error)
	Edit(category model.Category) (bool, error)
	Delete(id string) (bool, error)
}

func (repo *CategoryRepository) List() (categorys []*model.CategoryResult, err error) {
	var list []*model.CategoryResult
	if err := repo.DB.Raw("select c1.category_id as c1_category_id,c1.name as c1_name," +
		"c1.desc as c1_desc , c1.order as c1_order , c1.parent_id as c1_parent_id , c2.category_id as c2_category_id," +
		"c2.name as c2_name , c2.order as c2_order , c2.parent_id as c2_parent_id , c3.category_id as c3_category_id," +
		"c3.name as c3_name , c3.order as c3_order , c3.parent_id as c3_parent_id from category as c1 join category as c2 on c2.parent_id=c1.category_id " +
		"join category c3 on c3.parent_id = c2.category_id").Find(&list).Error; err != nil {
		return nil, err
	}
	return list, nil
}

func (repo *CategoryRepository) GetTotal() (total int64, err error) {
	if err := repo.DB.Raw("select count(c3.category_id) from category c1 join category c2 on c2.parent_id=c1.category_id" +
		" join category c3 on  c3.parent_id = c2.category_id ").Count(&total).Error; err != nil {
		return 0, err
	}
	return total, nil
}

func (repo *CategoryRepository) Get(id string) (categorys *model.CategoryResult, err error) {
	var list *model.CategoryResult
	if err := repo.DB.Raw("select c1.category_id as c1_category_id,c1.name as c1_name,"+
		"c1.desc as c1_desc , c1.order as c1_order , c1.parent_id as c1_parent_id , c2.category_id as c2_category_id,"+
		"c2.name as c2_name , c2.order as c2_order , c2.parent_id as c2_parent_id , c3.category_id as c3_category_id,"+
		"c3.name as c3_name , c3.order as c3_order , c3.parent_id as c3_parent_id from category as c1 join category as c2 on c2.parent_id=c1.category_id "+
		"join category c3 on c3.parent_id = c2.category_id where c3.category_id = ?", id).Find(&list).Error; err != nil {
		return nil, err
	}
	return list, nil
}

func (repo *CategoryRepository) Exist(Category model.Category) *model.Category {
	var category *model.Category
	if Category.Name != "" {
		repo.DB.Model(&category).Where("name = ?", Category.Name)
		return category
	}
	return nil
}

func (repo *CategoryRepository) ExistByCategoryId(id string ) *model.Category {
	var category *model.Category
	if id != "" {
		repo.DB.Model(&category).Where("category_id = ?", id)
		return category
	}
	return nil
}

func (repo *CategoryRepository) Add(category model.Category) (bool, error) {
	category.CreateAt, _ = time.Parse(utils.TimeLayout, utils.GetNow())
	category.UpdateAt, _ = time.Parse(utils.TimeLayout, utils.GetNow())
	if err := repo.DB.Create(&category).Error; err != nil {
		return false, errors.New("分类添加失败")
	}
	return true, nil
}

func (repo *CategoryRepository) Edit(category model.Category) (bool, error) {
	if category.CategoryId == "" {
		return false, errors.New("参数错误")
	}
	if err := repo.DB.Model(&category).Where("category_id = ?", category).Updates(map[string]interface{}{
		"name": category.Name, "order": category.Order, "parent_id": category.ParentId,
	}).Error; err != nil {
		return false, err
	}
	return true, nil
}

func (repo *CategoryRepository) Delete(id string) (bool, error) {
	var category *model.Category
	if id == "" {
		return false, errors.New("参数错误")
	}
	if err := repo.DB.Model(&category).Where("category_id = ?", id).Update("is_deleted", true).Error; err != nil {
		return false, err
	}
	return true, nil
}
